package com.hj.saapp.view;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.hj.saapp.R;
import com.hj.saapp.bean.MultipleListDialogBean;
import com.hj.saapp.common.manager.AppManager;
import com.hj.saapp.control.adapter.ListMultipleDialogAdapter;
import com.hj.saapp.control.listener.ListDialogListener;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by Administrator on 2015/10/16.
 */
@SuppressLint("ValidFragment")
public class ListMultipleDialog extends DialogFragment {

    ArrayList<MultipleListDialogBean>mData;
    String title;
    ListMultipleDialogAdapter adapter;
    ListDialogListener listener;
    private LinearLayout mViewDefaultingDialogMLinearLayout;
    private TextView mViewDefaultingDialogTitleTv;
    private ListView mViewDefaultingDialogContentLv;
    Button submit;

    private void assignViews(View view) {
        mViewDefaultingDialogMLinearLayout = (LinearLayout) view.findViewById(R.id.view_defaulting_dialog_mLinearLayout);
        mViewDefaultingDialogTitleTv = (TextView) view.findViewById(R.id.view_defaulting_dialog_titleTv);
        mViewDefaultingDialogContentLv = (ListView) view.findViewById(R.id.view_defaulting_dialog_contentLv);
        submit = (Button) view.findViewById(R.id.submit);
    }

    public ListMultipleDialog(String title, ArrayList<MultipleListDialogBean> mData, ListDialogListener listener)
    {
        this.mData = mData;
        this.title = title;
        this.listener = listener;
    }

    @Override
    public void onStart()
    {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels - 30, getDialog().getWindow().getAttributes().height);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.view_list_dialog,null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        assignViews(view);
        initView();
    }
    private void initView()
    {
        mViewDefaultingDialogTitleTv.setText(title);
        submit.setVisibility(View.VISIBLE);
        adapter = new  ListMultipleDialogAdapter(mData);
        mViewDefaultingDialogContentLv.setAdapter(adapter);
        mViewDefaultingDialogContentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mData.get(position).isSelect) {
                    mData.get(position).isSelect = false;
                } else {
                    mData.get(position).isSelect = true;
                }
                adapter.notifyDataSetChanged();

            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if (AppManager.getAppManager().getActivity().isFinishing())
                    {
                        return;
                    }
                    listener.onListItemClick(0);
                    ListMultipleDialog.this.dismissAllowingStateLoss();
                }
            }
        });

    }

    @Override
    public void onDetach() {

        super.onDetach();



        try {

            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");

            childFragmentManager.setAccessible(true);

            childFragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {

            throw new RuntimeException(e);

        } catch (IllegalAccessException e) {

            throw new RuntimeException(e);

        }

    }
}
